package cn.cai.controller;



import cn.cai.commonUtils.Result;
import cn.cai.service.OSSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 对象存储控制实例
 */
@RestController
@RequestMapping("/eduoss/fileoss")
@CrossOrigin
public class OSSController {
    @Autowired
    private OSSService ossService;
    /**
     * 用户通过接口上传文件，将文件存储进minio存储对象中，并返回文件的url地址
     * @param file  上传的要存储的文件
     * @return 带有url地址的结果对象
     */
    @PostMapping("/update")
    public Result update(MultipartFile file){
        String url = ossService.uploadFileAvatar(file);
        return Result.success().data("url",url);
    }

    @GetMapping("/defaultCover")
    public Result defaultCover(){
        String url = ossService.returnDefaultCover();
        return Result.success().data("url",url);
    }

    @PostMapping("/uploadCover")
    public Result uploadCover(@RequestBody MultipartFile file){
        String url = ossService.uploadCover(file);
        return Result.success().data("url",url);
    }
    /**
     * 返回默认头像
     */
    @GetMapping("/getDefaultAvatar")
    public Result getDefaultAvatar(){
        String url = ossService.getDefaultAvatar();
        return Result.success().data("url",url);
    }
    //返回指定桶中的指定对象
    @GetMapping("/objectInTheBucket/{bucketName}/{object}")
    public Result objectInTheBucket(@PathVariable("bucketName") String bucketName,
                                    @PathVariable("object") String object){
        String url = ossService.objectInTheBucket(bucketName,object);
        return Result.success();
    }
}
